﻿using CefSharp;
using CefSharp.WinForms;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame.Plugs
{
    public class MenuHandler : IContextMenuHandler
    {
        public Form MainForm { get; set; } = null;

        public bool OldTops = false;


        public void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
        {
            model.Clear();

            model.AddItem((CefMenuCommand)26501, "打开调试");
            model.AddItem((CefMenuCommand)26502, "关闭调试");
            model.AddItem((CefMenuCommand)26503, "重新加载");
            if (frame != null && frame != browser.MainFrame)
                model.AddItem((CefMenuCommand)26504, "重新加载框架");
            if (!string.IsNullOrEmpty(parameters.SelectionText))
                model.AddItem((CefMenuCommand)26505, "复制");
            if (!string.IsNullOrEmpty(Clipboard.GetText()))
                model.AddItem((CefMenuCommand)26506, "粘贴");
            if (!string.IsNullOrEmpty(parameters.SourceUrl) || !string.IsNullOrEmpty(parameters.LinkUrl))
                model.AddItem((CefMenuCommand)26507, "复制链接");

        }

        public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
        {
            if (commandId == (CefMenuCommand)26501)
            {
                if (MainForm != null)
                {
                    OldTops = MainForm.TopMost;
                    MainForm.TopMost = false;
                }
                browser.GetHost().ShowDevTools();
            }
            else if (commandId == (CefMenuCommand)26502)
            {
                if (MainForm != null)
                    MainForm.TopMost = OldTops;
                browser.GetHost().CloseDevTools();
            }
            else if (commandId == (CefMenuCommand)26503)
                browser.Reload(true);
            else if (commandId == (CefMenuCommand)26504)
                frame.EvaluateScriptAsync("window.location.reload(true)");
            else if (commandId == (CefMenuCommand)26505)
                frame.Copy();
            else if (commandId == (CefMenuCommand)26506)
                frame.Paste();
            else if (commandId == (CefMenuCommand)26507)
            {
                if (!string.IsNullOrEmpty(parameters.SourceUrl))
                    Clipboard.SetText(parameters.SourceUrl);
                else if (!string.IsNullOrEmpty(parameters.LinkUrl))
                    Clipboard.SetText(parameters.LinkUrl);
            }
            return true;
        }

        public void OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
        {
            //隐藏菜单栏
            var web = chromiumWebBrowser as ChromiumWebBrowser;
            Action setContextAction = () => web.ContextMenu = null;
            web.Invoke(setContextAction);
            //Dispatcher
        }

        public bool RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
        {
            return false;
        }
    }
}
